package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanException;
import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public void addnewBed(BedDto bedDto) {
        Bed bean = BeanUtil.toBean(bedDto, Bed.class);
        bean.setCreateTime(LocalDateTime.now());
        bean.setBedStatus(0);
        bean.setCreateBy(1l);
        try {
            bedMapper.addnewBed(bean);
        } catch (Exception e) {
            //手动抛异常对用户更加友好
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
    }

    @Override
    public BedVo selectById(Long id) {
        Bed bed = bedMapper.selectById(id);
        BedVo bean = BeanUtil.toBean(bed, BedVo.class);
        return bean;
    }

    @Override
    public void update(BedDto bedDto) {
        Bed bean = BeanUtil.toBean(bedDto, Bed.class);
        bean.setUpdateBy(1L);
        bean.setUpdateTime(LocalDateTime.now());
        if (ObjectUtil.isNotEmpty(bedDto.getBedStatus())){
            bean.setBedStatus(bedDto.getBedStatus());
        }
        bedMapper.update(bean);

    }

    @Override
    public void deleteBad(Long id) {
        bedMapper.deleteBad(id);
    }

}

